package cc.rengu.igas.smps.biz.service.impl;

import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.common.enums.RespCodeEnum;
import cc.rengu.igas.smps.facade.BankSupService;
import cc.rengu.igas.smps.facade.request.*;
import cc.rengu.igas.smps.facade.response.*;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by WKF on 2020-04-08
 *
 * @author WKF
 */
public class BankSupServiceImpl implements BankSupService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<BankCardStatusQueryResponse> bankCardStatusQuery(BankCardStatusQueryRequest bankCardStatusQueryrequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankCardStatusQueryResponse> result = new Result<>();
        BankCardStatusQueryResponse bankCardStatusQueryResponse = new BankCardStatusQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankCardStatusQueryrequest, SmpsServiceConstant.BANK_CARD_STATUS_QUERY_SERVICE, BankCardStatusQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankCardStatusQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银行卡状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bankCardStatusQueryResponse.setRespCode(((BizException) e).getCode());
                bankCardStatusQueryResponse.setRespDesc(e.getMessage());
            } else {
                bankCardStatusQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bankCardStatusQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<BankCardPasswdVerifyResponse> bankCardPasswdVerify(BankCardPasswdVerifyRequest bankCardPasswdVerifyRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankCardPasswdVerifyResponse> result = new Result<>();
        BankCardPasswdVerifyResponse bankCardPasswdVerifyResponse = new BankCardPasswdVerifyResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankCardPasswdVerifyRequest, SmpsServiceConstant.BANK_CARD_PASSWORD_VERIFY_SERVICE, BankCardPasswdVerifyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankCardPasswdVerifyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行卡密码校验失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bankCardPasswdVerifyResponse.setRespCode(((BizException) e).getCode());
                bankCardPasswdVerifyResponse.setRespDesc(e.getMessage());
            } else {
                bankCardPasswdVerifyResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bankCardPasswdVerifyResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<BankFourElementVerifyResponse> bankFourElementVerify(BankFourElementVerifyRequest bankFourElementVerifyRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankFourElementVerifyResponse> result = new Result<>();
        BankFourElementVerifyResponse bankFourElementVerifyResponse = new BankFourElementVerifyResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankFourElementVerifyRequest, SmpsServiceConstant.BANK_FOUR_ELEMENT_VERIFY_SERVICE, BankFourElementVerifyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankFourElementVerifyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行卡四要素校验失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bankFourElementVerifyResponse.setRespCode(((BizException) e).getCode());
                bankFourElementVerifyResponse.setRespDesc(e.getMessage());
            } else {
                bankFourElementVerifyResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bankFourElementVerifyResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<BankCardSignResponse> bankCardSign(BankCardSignRequest bankCardSignRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankCardSignResponse> result = new Result<>();
        BankCardSignResponse bankCardSignResponse = new BankCardSignResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankCardSignRequest, SmpsServiceConstant.BANK_CARD_SIGN_SERVICE, BankCardSignResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankCardSignResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行卡签约失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bankCardSignResponse.setRespCode(((BizException) e).getCode());
                bankCardSignResponse.setRespDesc(e.getMessage());
            } else {
                bankCardSignResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bankCardSignResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<BankAcctInfoQueryResponse> bankAcctInfoQuery(BankAcctInfoQueryRequest bankAcctInfoQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankAcctInfoQueryResponse> result = new Result<>();
        BankAcctInfoQueryResponse bankAcctInfoQueryResponse = new BankAcctInfoQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankAcctInfoQueryRequest, SmpsServiceConstant.BANK_ACCT_INFO_QUERY_SERVICE, BankAcctInfoQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankAcctInfoQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银行卡账户信息查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bankAcctInfoQueryResponse.setRespCode(((BizException) e).getCode());
                bankAcctInfoQueryResponse.setRespDesc(e.getMessage());
            } else {
                bankAcctInfoQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bankAcctInfoQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CreditCardSignRightResponse> creditCardSignRight(CreditCardSignRightRequest creditCardSignRightRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CreditCardSignRightResponse> result = new Result<>();
        CreditCardSignRightResponse creditCardSignRightResponse = new CreditCardSignRightResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(creditCardSignRightRequest, SmpsServiceConstant.CREDIT_CARD_SIGN_RIGHT_SERVICE, CreditCardSignRightResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CreditCardSignRightResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("贷记卡签权失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                creditCardSignRightResponse.setRespCode(((BizException) e).getCode());
                creditCardSignRightResponse.setRespDesc(e.getMessage());
            } else {
                creditCardSignRightResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                creditCardSignRightResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
